package dynamicprogramming;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/2 12:54
 * https://leetcode-cn.com/problems/min-cost-climbing-stairs/
 */
public class MinCostClimbingStairs {

    /**
     * dp[i] 表示爬到第i层所需要的费用
     * 地推公式：dp[i] = Math.min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])
     * 初始化：  dp[0] = 0 dp[1] = 0 dp[2] = Math.min(cost[0], cost[1])
     * @param cost
     * @return
     */
    public static int minCostClimbingStairs(int[] cost) {
        //length > 3
        int[] dp = new int[cost.length + 1];
        dp[0] = 0;
        dp[1] = 0;
        for (int i = 2; i <= cost.length; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[cost.length];
    }

    public static void main(String[] args) {
        int[] cost = {1,100,1,1,1,100,1,1,100,1};
        int i = minCostClimbingStairs(cost);
        System.out.println(i);
    }
}
